<template>
  <div style="width: 100%; height: 100%; background: lightgrey; padding: 10px">
    <div
      style="width: 100%; margin: 0 auto; padding: 20px; min-height: 623px"
      class="body"
    >
      <div style="width: 80%; margin: 0 auto">
        <el-button @click="handleCreate" type="primary">新建管理员</el-button>
        <el-table :data="tableData" v-loading="listLoading">
          <el-table-column prop="username" label="管理员用户名" min-width="300" >
          </el-table-column>
          <el-table-column label="操作" min-width="300">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog
        :title="type ? '新建管理员' : '编辑管理员信息'"
        :visible.sync="dialogVisible"
      >
        <el-form
          :model="form"
          label-position="left"
          style="width: 80%; margin: 0 auto"
        >
          <el-form-item label="用户名" label-width="100px">
            <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              :disabled="!type"
            ></el-input>
          </el-form-item>
          <el-form-item v-if="!type" label="旧密码" label-width="100px">
            <el-input
              v-model="form.oldPwd"
              placeholder="请输入旧密码"
              show-password
              v-if="!type"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" label-width="100px">
            <el-input
              v-model="form.password"
              placeholder="请输入新密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" label-width="100px">
            <el-input
              v-model="form.confirmPwd"
              placeholder="请再次输入新密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSubmit">提交</el-button>
            <el-button type="primary" @click="handleCancel">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!-- 请输入用户名 -->
      <!-- 请输入密码 -->
      <!-- 请再次输入密码 -->
    </div>
  </div>
</template>

<style scoped>
.body {
  min-height: 400px;
  background-color: white;
  border: indigo 2px;
  /* padding: 10px; */
  /* -webkit-box-shadow: -4px 7px 46px 2px rgba(0, 0, 0, 0.1);
  -moz-box-shadow: -4px 7px 46px 2px rgba(0, 0, 0, 0.1);
  -o-box-shadow: -4px 7px 46px 2px rgba(0, 0, 0, 0.1);
  box-shadow: -4px 7px 46px 2px rgba(0, 0, 0, 0.1); */
  /* border-radius: 5px; */
}
</style>

<script>
import md5 from 'js-md5'
import { getAdminList, addAdmin, deleteAdmin, editAdmin } from '@/api/user'
export default ({
  data() {
    return {
      listLoading: false,
      dialogVisible: false,
      type: true,//true为创建管理员，false为编辑管理员信息
      form: {
        username: '',
        password: '',
        confirmPwd: '',
        oldPwd: ''
      },
      tableData: [{
        username: ''
      }]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getAdminList().then(response => {
        this.tableData = response.data
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    handleCreate() {
      this.form.username = ''
      this.form.password = ''
      this.form.confirmPwd = ''
      this.type = true
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.form.username = row.username
      this.form.password = ''
      this.form.oldPwd = ''
      this.form.confirmPwd = ''
      this.type = false
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('您确定要删除该管理员嘛？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let data = { username: row.username }
        deleteAdmin(data).then(response => {
          this.$message({
            type: 'success',
            message: '删除管理员 ' + row.username + ' 成功'
          })
          this.getList();
        })
      }).catch(() => {
        //do nothing
      });
    },
    handleSubmit() {
      if (this.form.username === '' || this.form.password === '') {
        this.$message({
          type: 'error',
          message: '用户名、密码不能为空'
        })
        return;
      }

      if (this.form.password !== this.form.confirmPwd) {
        this.$message({
          type: 'error',
          message: '两次密码输入不一致，请重新输入'
        })

        this.form.password = ''
        this.form.confirmPwd = ''
        return;
      }

      if (this.type) {
        //创建管理员
        let formdata = new FormData()
        formdata.append('username', this.form.username)
        formdata.append('password', md5(this.form.password))

        addAdmin(formdata).then(response => {
          if (response.code == 1) {
            this.$message({
              type: 'success',
              message: '新建管理员 ' + this.form.username + ' 成功'
            })
            //更新用户列表
            this.getList()
            //将form中的user，password，confirmPwd重新初始化
            this.dialogVisible = !this.dialogVisible
            this.form.username = ''
            this.form.password = ''
            this.form.confirmPwd = ''
          }
        }).catch(err => {
          console.log(err)
        })
      } else {
        //修改管理员密码
        let formdata = new FormData()
        formdata.append('username', this.form.username)
        formdata.append('password', md5(this.form.oldPwd))
        formdata.append('newPassword', md5(this.form.password))

        editAdmin(formdata).then(response => {
          if (response.code == 1) {
            this.$message({
              type: 'success',
              message: '编辑管理员 ' + this.form.username + ' 的密码成功'
            })
            //更新用户列表
            this.getList()
            //将form中的user，password，confirmPwd重新初始化
            this.dialogVisible = !this.dialogVisible
            this.form.username = ''
            this.form.password = ''
            this.form.confirmPwd = ''
          }
        })
      }
    },
    handleCancel() {
      this.$confirm('此操作将放弃所有已填内容，确定取消嘛？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.dialogVisible = !this.dialogVisible
        this.form.username = ''
        this.form.password = ''
        this.form.confirmPwd = ''
      }).catch(() => {
        //do nothing
      });
    }
  }
})
</script>